package net.homelinux.chaoswg.io.renga.core.gui.operations.helper;

import java.util.Observable;

import net.homelinux.chaoswg.io.renga.core.gui.operations.Operation;

public class Connection extends  Observable implements RemovalListener {
   protected Operation inputOperation = null;
   protected int output = -1;
    
    public boolean isConnected () {
        return inputOperation != null;
    }
    
    public Object getResult () {
        return isConnected() ? inputOperation.apply(output) : "0";      
    }

    public Operation getInputOperation() {
        return inputOperation;
    }

    public void setInputOperation(final Operation inputOperation) {
        if (this.inputOperation != null) {
            this.inputOperation.removeRemovalListener(this);
        }
        this.inputOperation = inputOperation;
        this.inputOperation.addRemovalListener(this);
        
        setChanged();
        notifyObservers(this);
    }

    public int getOutput() {
        return output;
    }

    public void setOutput(final int output) {
        this.output = output;

        setChanged();
        notifyObservers(this);
    }
    
    public void clean () {
        output = -1;
        inputOperation = null;

        setChanged();
        notifyObservers(this);
  }

    public void operationRemoved(final Operation op) {
        if (op == this.inputOperation) {
            this.clean();
        }
    }
}
